home *** CD-ROM | disk | FTP | other *** search
- /* Main.c */
- /*
- * List In A List Sample
- * Main.c
- * Copyright © 1993-94 Apple Computer Inc.
- */
- #define EXTERN /* Allocate variables */
- #include "ListInAList.h"
- #ifdef __powerc
- /*
- * Power PC does not automatically define the QuickDraw globals. This is because
- * only "application" code-fragments need these globals, and this cannot be
- * determined before the code fragment is constructed.
- */
- QDGlobals qd; /* This is not automatically defined on PowerPC */
- #endif
-
- /*
- * These enum's define items in the various menus.
- */
- enum AppleMenu {
- kAppleAbout = 1
- };
- enum FileMenu {
- kFileQuit = 1
- };
- enum EditMenu {
- kEditUndo = 1,
- kEditUnused,
- kEditCut,
- kEditCopy,
- kEditPaste,
- kEditClear
- };
-
- Boolean gInForeground;
-
- /*
- * Local function prototypes.
- */
- void main(void);
- void EventLoop(void);
- void ApplicationEventLoop(void);
- void DoMouseEvent(void);
- void ProcessActivateEvent(
- WindowPtr activeWindow,
- Boolean isActivate
- );
- void DoCommand(
- WindowPtr activeWindow,
- long menuChoice
- );
- void AdjustMenus(void);
- void InitMacintosh(void);
- void InitApplication(void);
- void AdjustEditMenu(
- Boolean isDeskAcc
- );
- static Boolean IsOurWindow(
- WindowPtr theWindow
- );
-
- /*
- * main
- * The application main program. This is a limited program for the twist-down
- * sample; it is not intended as a model for a complete Macintosh program.
- */
- void
- main()
- {
- InitMacintosh();
- InitApplication();
- gInForeground = TRUE;
- MakeHFSBrowserWindow();
- InitCursor();
- while (gQuitNow == FALSE)
- EventLoop();
- ExitToShell();
- }
-
- /*
- * Application event loop: process one event each time.
- */
- void
- EventLoop(void)
- {
- long menuChoice;
- register WindowPtr theWindow;
- GrafPtr savePort;
- Boolean isActivating;
-
- if (gUpdateMenusNeeded)
- AdjustMenus();
- WaitNextEvent(
- everyEvent,
- &gEventRecord,
- (gInForeground) ? 10L : 60L,
- NULL
- );
- theWindow = FrontWindow();
- switch (gEventRecord.what) {
- case nullEvent:
- break;
- case keyDown:
- case autoKey:
- if ((gEventRecord.message & charCodeMask) == '.'
- && (gEventRecord.modifiers & cmdKey) != 0) {
- FlushEvents(keyDown | autoKey, 0);
- gQuitNow = TRUE;
- }
- else if ((gEventRecord.modifiers & cmdKey) != 0) {
- if (gEventRecord.what == keyDown) {
- menuChoice = MenuKey(gEventRecord.message & charCodeMask);
- if (HiWord(menuChoice) != 0 && IsOurWindow(theWindow))
- DoCommand(theWindow, menuChoice);
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((BrowserPtr) theWindow);
- else {
- SysBeep(10);
- }
- }
- }
- else if (IsOurWindow(theWindow))
- DoWindowKeyDown((BrowserPtr) theWindow);
- else {
- SysBeep(10);
- }
- break;
- case mouseDown:
- DoMouseEvent();
- break;
- case updateEvt:
- theWindow = (WindowPtr) gEventRecord.message;
- GetPort(&savePort);
- SetPort(theWindow);
- BeginUpdate(theWindow);
- EraseRect(&theWindow->portRect);
- if (IsOurWindow(theWindow) == FALSE)
- DrawControls(theWindow);
- else {
- UpdateBrowserWindow((BrowserPtr) theWindow, theWindow->visRgn);
- }
- EndUpdate(theWindow);
- SetPort(savePort);
- break;
- case activateEvt:
- theWindow = (WindowPtr) gEventRecord.message;
- isActivating = ((gEventRecord.modifiers & activeFlag) != 0);
- goto activateEvent;
- break;
- case osEvt:
- switch (((unsigned long) gEventRecord.message) >> 24) {
- case mouseMovedMessage:
- break;
- case suspendResumeMessage:
- isActivating = ((gEventRecord.message & 0x01) != 0);
- activateEvent: if (isActivating) {
- /*
- * Activate this window. Activate events define theWindow from
- * the event record, while suspend/resume uses the pre-set
- * FrontWindow value.
- */
- SelectWindow(theWindow);
- (void) TEFromScrap();
- }
- if (IsOurWindow(theWindow)) {
- ActivateBrowser(
- (BrowserPtr) theWindow,
- isActivating
- );
- /*
- * Globalize the current window for the debugger's convenience.
- */
- if (isActivating)
- gCurrentBrowserPtr = (BrowserPtr) theWindow;
- }
- else {
- /* Desk accessory or what? */
- }
- gInForeground = isActivating;
- gUpdateMenusNeeded = TRUE;
- break;
- }
- break;
- }
- }
-
- /*
- * DoMouseEvent
- * The user clicked on something. Handle application-wide processing here, or call
- * a Browser function for specific action.
- */
- void
- DoMouseEvent(void)
- {
- WindowPtr theWindow;
- short whichPart;
-
- whichPart = FindWindow(gEventRecord.where, &theWindow);
- if (theWindow == NULL)
- theWindow = FrontWindow();
- if (whichPart == inMenuBar && IsOurWindow(theWindow) == FALSE)
- theWindow = FrontWindow();
- switch (whichPart) {
- case inDesk:
- break;
- case inMenuBar:
- InitCursor();
- DoCommand(theWindow, MenuSelect(gEventRecord.where));
- break;
- case inDrag:
- DragWindow(theWindow, gEventRecord.where, &qd.screenBits.bounds);
- break;
- case inGoAway:
- if (TrackGoAway(theWindow, gEventRecord.where)) {
- if (IsOurWindow(theWindow)) {
- DisposeBrowser((BrowserPtr) theWindow);
- if (gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- }
- else {
- SysBeep(10);
- }
- }
- break;
- case inContent:
- if (theWindow != FrontWindow())
- SelectWindow(theWindow);
- else if (IsOurWindow(theWindow)) {
- DoContentClick((BrowserPtr) theWindow);
- }
- else {
- /* Nothing happens here */
- }
- break;
- default:
- break; /* Bogus click: ignore */
- }
- }
-
- /*
- * DoCommand
- * Process a menu or keystroke command.
- */
- void
- DoCommand(
- WindowPtr theWindow,
- long menuChoice
- )
- {
- short menuItem;
- Str255 menuText;
- GrafPtr savePort;
-
- menuItem = LoWord(menuChoice);
- switch (HiWord(menuChoice)) {
- case MENU_Apple:
- if (menuItem == kAppleAbout)
- ; /* Nothing happens here */
- else {
- GetItem(gAppleMenu, menuItem, menuText);
- AdjustEditMenu(TRUE);
- GetPort(&savePort);
- OpenDeskAcc(menuText);
- SetPort(savePort);
- AdjustEditMenu(IsOurWindow(theWindow) == FALSE);
- }
- break;
- case MENU_File:
- switch (menuItem) {
- case kFileQuit:
- gQuitNow = TRUE;
- break;
- }
- break;
- case MENU_Edit:
- if (SystemEdit(menuItem - 1) == FALSE)
- SysBeep(10);
- break;
- }
- HiliteMenu(0);
- }
-
- /*
- * AdjustMenus
- * Enable/disable menu options.
- */
- void
- AdjustMenus(void)
- {
- EnableItem(gFileMenu, kFileQuit);
- AdjustEditMenu(IsOurWindow(FrontWindow()) == FALSE);
- }
-
- /*
- * AdjustEditMenu
- * Enable/disable Edit Menu options.
- */
- void
- AdjustEditMenu(
- Boolean isDeskAcc
- )
- {
- if (isDeskAcc) {
- EnableItem(gEditMenu, kEditUndo);
- EnableItem(gEditMenu, kEditCut);
- EnableItem(gEditMenu, kEditCopy);
- EnableItem(gEditMenu, kEditPaste);
- EnableItem(gEditMenu, kEditClear);
- }
- else {
- DisableItem(gEditMenu, kEditUndo);
- DisableItem(gEditMenu, kEditCut);
- DisableItem(gEditMenu, kEditCopy);
- DisableItem(gEditMenu, kEditPaste);
- DisableItem(gEditMenu, kEditClear);
- }
- }
-
- /*
- * InitMacintosh
- * Perform the normal application initialization. This must be extended for "real"
- * applications. The only thing this module does is initialize the managers.
- */
- void
- InitMacintosh(void)
- {
- int i;
-
- MaxApplZone();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(NULL);
- HNoPurge((Handle) GetCursor(watchCursor));
- SetCursor(*GetCursor(watchCursor));
- for (i = 0; i < 3; i++)
- EventAvail(everyEvent, &gEventRecord);
- }
-
- /*
- * InitApplication
- * Continue initialization.
- */
- void
- InitApplication(void)
- {
- (void) TEFromScrap();
- SetMenuBar(GetNewMBar(MBAR_MenuBar));
- gAppleMenu = GetMHandle(MENU_Apple);
- gFileMenu = GetMHandle(MENU_File);
- gEditMenu = GetMHandle(MENU_Edit);
- AddResMenu(GetMHandle(MENU_Apple), 'DRVR');
- DrawMenuBar();
- SetupAnimatedCursor(ACUR_Animator);
- }
-
- /*
- * IsOurWindow
- * Return TRUE if this is a browser window.
- */
- Boolean
- IsOurWindow(
- WindowPtr theWindow
- )
- {
- if (theWindow == NULL
- || ((WindowPeek) theWindow)->windowKind != userKind)
- return (FALSE);
- else {
- return (TRUE);
- }
- }
-
- void pstrcpy(
- StringPtr dst,
- StringPtr src
- )
- {
- BlockMove(src, dst, src[0] + 1);
- }
-
- void pstrcat(
- StringPtr dst,
- StringPtr src
- )
- {
- short length;
-
- length = 255 - dst[0];
- if (length > src[0])
- length = src[0];
- BlockMove(&src[1], &dst[1] + dst[0], length);
- dst[0] += length;
- }
-